// Author: Domien Nowicki


#ifndef WORLD_H
#define WORLD_H

#include "TextureManager.h"
#include "Map.h"
#include "Scalar.h"
#include "Vector.h"
#include <string>
#include <stdio.h>

using namespace std;

class World
{
public:
	World();
	virtual ~World();

	void Clear();
	virtual void Reset();

	bool IsEmptyCell(unsigned int column, unsigned int row) const;
	void SetWallCell(unsigned int column, unsigned int row);
	void SetEmptyCell(unsigned int column, unsigned int row);
	void SetTextureCell(unsigned int column, unsigned int row, Cell::Side side, unsigned int tindex);
	unsigned int AddTexture(const Texture& texture);
	void RemoveTexture(unsigned int tindex);

	void SetFloorTexture(unsigned int tindex);
	void SetCeilingTexture(unsigned int tindex);
	void SetDefaultTexture(unsigned int tindex);

	void ChangeUserColor(unsigned int tindex, const Pixel& userColor);

	unsigned int GetTextureCell(unsigned int column, unsigned int row, Cell::Side side) const;
	const Texture& GetTexture(unsigned int tindex) const;


	void Resize(unsigned int newColumnCount, unsigned int newRowCount);

	unsigned int GetHeight() const;
	unsigned int GetWidth() const;
	unsigned int GetMapHeight() const;
	unsigned int GetMapWidth() const;

	const Texture& GetFloorTexture() const;
	const Texture& GetCeilingTexture() const;
	unsigned int GetFloorTextureIndex() const;
	unsigned int GetCeilingTextureIndex() const;
	unsigned int GetDefaultTextureIndex() const;
	bool HasChanged() const;

	void SaveToFile(const std::string& fileName);
	virtual void LoadFromFile(const std::string& fileName);

	World& operator=(const World &rhs);

	const Vector& GetCameraPos() const;
	const Scalar& GetCameraAngle() const;
	bool HasPlacedCamera() const;

	virtual void PlaceCamera(const Vector& pos, const Scalar& angle);
	virtual void RemoveCamera();
protected:
	// Constructor only for inheriting classes
	World(Map *nMap, TextureManager *nTextureManager);
	virtual void SetChanged();
	virtual void SetUnchanged();
	unsigned int CalculateTextureIndex(unsigned int removeTextureIndex, unsigned int tindex);

	TextureManager *textures;
	Map *map;
private:
	void SaveTexturesToFile(FILE* file);
	void SaveMapToFile(FILE* file);

	bool LoadTexturesFromFile(FILE* file);
	bool LoadMapFromFile(FILE* file);

	

	unsigned int floorTexture, ceilingTexture, defaultTexture;
	bool changed;
	Scalar cameraAngle;
	Vector cameraPos;
	bool cameraPlaced;

	static const unsigned int RGBSIZE = 3;

	static const unsigned long TEXTURETAG = 1;	
	static const unsigned long MAPTAG = 2;

	static const unsigned long SOLIDTYPE = 1;
	static const unsigned long BITMAPTYPE = 0;
};

#endif //WORLD_H

